package com.gzgy.ustore.api.service.impl;

import com.gzgy.ustore.api.common.exception.BaseAPIException;
import com.gzgy.ustore.api.common.exception.ExceptionCodeStatusEnum;
import com.gzgy.ustore.api.common.util.DateUtil;
import com.gzgy.ustore.api.common.util.I18nUtil;
import com.gzgy.ustore.api.dao.ParamTypeDao;
import com.gzgy.ustore.api.model.ParamTypeList;
import com.gzgy.ustore.api.service.ParamTypeService;
import com.ustore.model.ParamType;
import org.apache.commons.lang.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ParamTypeServiceImpl implements ParamTypeService {

    @Autowired
    private ParamTypeDao paramTypeDao;

    @Override
    public String addParamType(String paramType, String paramKey, String companyId, String userId, String paramValue, String desc) {
        //如果存在 则保存 paramType,paramKey,companyId 组成唯一记录
        int count = paramTypeDao.getParamType(paramType,paramKey,companyId);
        if(count > 0) {
            int errorCode = ExceptionCodeStatusEnum.PARAM_TYPE_IS_EXISTS_EXCEPTION.toInt();
            String errorMsg = I18nUtil.getMessage(errorCode,null,"zh_CN");
            throw new BaseAPIException(errorMsg,errorCode);
        }
        //添加记录
        ParamType p = new ParamType();
        p.setParamKey(paramKey);
        p.setCompanyId(NumberUtils.toInt(companyId));
        p.setParamDesc(desc);
        p.setType(paramType);
        p.setParamValue(paramValue);
        p.setCreatedTime(DateUtil.getCurrentDate());
        p.setCreatedBy(userId);
        return paramTypeDao.add(p).getId().toString();
    }

    @Override
    public String updateParamType(String flag, String userId, String paramId, String paramValue, String desc) throws SQLException {
        //找不到param type 抛出异常
        ParamType p  = paramTypeDao.get(paramId);
        if(p  == null){
            int errorCode = ExceptionCodeStatusEnum.PARAM_TYPE_NOTE_FOUND_EXCEPTION.toInt();
            String errorMsg = I18nUtil.getMessage(errorCode,null,"zh_CN");
            throw new BaseAPIException(errorMsg,errorCode);
        }
        //更新信息
        if("0".equals(flag)){
            p.setParamValue(paramValue);
            p.setParamDesc(desc);
            p.setUpdatedBy(userId);
            p.setUpdatedTime(DateUtil.getCurrentDate());
            paramTypeDao.update(p);
        }else {
            p.setUpdatedBy(userId);
            p.setUpdatedTime(DateUtil.getCurrentDate());
            p.setIsDeleted(true);
            paramTypeDao.update(p);
        }

        return "操作成功";
    }

    @Override
    public Map<String,Object> paramTypeList(String companyId, String index, String page, String paramKey, String type) {

        Map<String,Object> map = new HashMap<String,Object>();
        int count  = 0 ;
        count = paramTypeDao.countParamTypeList(companyId,type,paramKey);
        map.put("paramTypeList",new ArrayList<ParamTypeList>());
        if(count > 0){
            List<ParamTypeList> lists = paramTypeDao.paramTypeList(companyId,index,page,type,paramKey);
            map.put("paramTypeList",lists);
        }
        map.put("count",count);
        map.put("index",index);
        return map;
    }
}
